package org.chen.aop;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.indices.RefreshRequest;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Aspect
@Component
public class ElasticsearchRefreshAspect {
    
    @Autowired
    private ElasticsearchClient esClient;
    
    @After("@annotation(RefreshES)")
    public void refreshIndex() throws IOException {
        RefreshRequest request = RefreshRequest.of(i -> i.index("articles"));
        esClient.indices().refresh(request);
    }
}